'''
Created on Jul 20, 2010

@author: behrooz
'''
from django.db import models
import datetime

class PaymentCreditManager(models.Manager):
    
    def can_pay(self, user, amount):
        from amscms.payment.models import PaymentCredit, UserPaymentPolicy
        if UserPaymentPolicy.objects.has_free_policy(user):
            return True
        credit, created = PaymentCredit.objects.get_or_create(user=user)
        return credit >= amount
    
    def get_credit(self, user):
        from amscms.payment.models import PaymentCredit
        credit, created = PaymentCredit.objects.get_or_create(user=user)
        return credit.balance
    
    def pay_order(self, user, adorder_id):
        from amscms.payment.models import PaymentCredit, PaymentCreditHistory, UserPaymentPolicy
        from amscms.ams.models import Ad, AdOrder
        the_credit, created = PaymentCredit.objects.get_or_create(user=user)
        adorder = AdOrder.objects.get(pk=adorder_id)
        if the_credit is None or adorder is None:
            return None
        freepolicy = UserPaymentPolicy.objects.has_free_policy(user)
        if freepolicy:
            amount = 0
        else:
            amount = adorder.amount
            if not self.can_pay(user, amount):
                return None
        # payment history
        history = PaymentCreditHistory(credit=the_credit, order=adorder, amount= -amount)
        history.save()
        # credit balance
        the_credit.balance = the_credit.balance - amount
        the_credit.save()
        # update order
        adorder.paid = True
        adorder.payment_date = datetime.datetime.now()
        adorder.save()
        # update the ad
        ad = adorder.ad
        Ad.objects.enable_ad(ad)
        
        return history
    
class CreditDepositHistoryManager(models.Manager):
    
    def deposit_credit(self, user, amount):
        from amscms.payment.models import PaymentCredit, CreditDepositHistory
        credit, created = PaymentCredit.objects.get_or_create(user=user)
        deposit = CreditDepositHistory(credit=credit, amount=amount)
        credit.balance = credit.balance + amount
        deposit.save()
        credit.save()
        return deposit
    
    def find_deposit_history(self, user):
        from amscms.payment.models import CreditDepositHistory
        deposits = CreditDepositHistory.objects.filter(credit__user=user)
        return deposits
    
class PaymentCreditHistoryManager(models.Manager):
    
    def find_payment_history(self, user):
        from amscms.payment.models import PaymentCreditHistory
        history = PaymentCreditHistory.objects.filter(credit__user=user)
        return history
    
class UserPaymentPolicyManager(models.Manager):
    
    def find_payment_policies(self, user):
        from amscms.payment.models import UserPaymentPolicy
        policies = UserPaymentPolicy.objects.filter(user=user)
        return policies
    
    def has_free_policy(self, user):
        policies = self.find_payment_policies(user)
        if policies is None or len(policies) == 0:
            return False
        for p in policies:
            if p.policy.isfree:
                return True
        return False
        
